package com.jcark.ui.graphics.style;

import android.annotation.SuppressLint;
import android.graphics.LinearGradient;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;

/**
 * Description: 带阴影的Drawable的工具类<br>
 *
 * @author King.Zi<br>
 * @date 2023/3/2<br>
 * Email: 78688689@qq.com
 */
public class ShadowDrawableHelper {

    public static class Builder {
        @ColorInt
        private int mColor;
        @ColorInt
        private int mShadowColor;

        private int[] mGradientColorArray;
        @Nullable
        private float[] mGradientPositions;

        private LinearGradient mLinearGradient;
        private int mRadius;
        private int mShadowRadius;

        private int mOffsetX;
        private int mOffsetY;

        @SuppressLint("ResourceAsColor")
        public Builder() {
            //初始化默认值
            mColor = com.google.android.material.R.color.primary_material_dark;
            mShadowColor = com.google.android.material.R.color.primary_text_disabled_material_dark;
            mRadius = 10;
            mShadowRadius = 16;
            mOffsetX = 0;
            mOffsetY = 0;
        }

        public Builder setColor(@ColorInt int color) {
            this.mColor = color;
            return this;
        }

        /**
         * 设置阴影颜色
         *
         * @param shadowColor
         */
        public Builder setShadowColor(@ColorInt int shadowColor) {
            this.mShadowColor = shadowColor;
            return this;
        }

        /**
         * 设置渐变数组，默认从左到右渐变
         *
         * @param colorArray 颜色数组
         */
        public Builder setGradientColorArray(@Nullable int[] colorArray) {
            this.mGradientColorArray = colorArray;
            return this;
        }

        /**
         * 设置渐变颜色位置，与的渐变色数组配合使用，指定每个颜色的位置（注意必须与setGradientColorArray颜色数组长度一致）
         *
         * @param positions 渐变颜色位置
         */
        public Builder setGradientPositions(@Nullable float[] positions) {
            this.mGradientPositions = positions;
            return this;
        }

        /**
         * 设置一个线性渐变，当默认（从左到右渐进）的需求无法满足时
         *
         * @param linearGradient 线性渐变
         */
        public Builder setLinearGradient(@Nullable LinearGradient linearGradient) {
            this.mLinearGradient = linearGradient;
            return this;
        }

        /**
         * 设置圆角半径
         *
         * @param radius 圆角半径
         */
        public Builder setRadius(int radius) {
            this.mRadius = radius;
            return this;
        }

        /**
         * 设置阴影圆角半径
         *
         * @param shadowRadius 阴影圆角半径
         */
        public Builder setShadowRadius(int shadowRadius) {
            this.mShadowRadius = shadowRadius;
            return this;
        }

        /**
         * 设置X阴影偏移量
         *
         * @param offsetX
         */
        public Builder setOffsetX(int offsetX) {
            this.mOffsetX = offsetX;
            return this;
        }

        /**
         * 设置Y阴影偏移量
         *
         * @param offsetY
         */
        public Builder setOffsetY(int offsetY) {
            this.mOffsetY = offsetY;
            return this;
        }

        public ShadowDrawable builder() {
            return new ShadowDrawable(mColor, mGradientColorArray, mGradientPositions, mShadowColor, mLinearGradient,
                    mRadius, mShadowRadius, mOffsetX, mOffsetY);
        }
    }

    public static void setShadowDrawable(View view, Builder builder) {
        //关闭硬件加速
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ViewCompat.setBackground(view, builder.builder());
    }
}
